package com.kikoulol.jarmoryextractor;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

import com.kikoulol.jarmoryextractor.utils.Utils;

public class Character {

	private Element characterXml;

	private CharacterStats stats;
	private List<ArenaTeam> arenaTeams = new ArrayList<ArenaTeam>();
	private List<Item> items = new ArrayList<Item>();

	private String charUrl;
	private String name;
	private String titleId;
	private String teamRank;
	private String suffix;
	private String seasonGamesWon;
	private String seasonGamesPlayed;
	private String realm;
	private String raceId;
	private String race;
	private String prefix;
	private String points;
	private String level;
	private String lastModified;
	private String guildUrl;
	private String guildName;
	private String guildId;
	private String guild;
	private String genderId;
	private String gender;
	private String gamesWon;
	private String gamesPlayed;
	private String factionId;
	private String faction;
	private String classUrl;
	private String classId;
	private String classType;
	private String battleGroup;

	private String armoryUrl;

	public Character(String url) throws IOException, ParserConfigurationException, SAXException {
		armoryUrl = "http://" + url.split("/")[2] + "/";
		String xml = Utils.getArmoryXml(url);
		try {
			characterXml = Utils.getXmlRootElement(xml);
		}
		catch (Exception e) {
			File fileToRemove = new File(Utils.getLocalUrl(url));
			fileToRemove.delete();
			xml = Utils.getArmoryXml(url);
			characterXml = Utils.getXmlRootElement(xml);
		}
		init(characterXml);
	}

	private void init(Element characterXml) {
		
		Node character = characterXml.getElementsByTagName("characterInfo").item(0).getChildNodes().item(1);

		setName(character.getAttributes().getNamedItem("name").getNodeValue());
		setBattleGroup(character.getAttributes().getNamedItem("battleGroup").getNodeValue());
		setCharUrl(character.getAttributes().getNamedItem("charUrl").getNodeValue());
		setClassId(character.getAttributes().getNamedItem("classId").getNodeValue());
		setClassType(character.getAttributes().getNamedItem("class").getNodeValue());
		setGender(character.getAttributes().getNamedItem("gender").getNodeValue());
		setGenderId(character.getAttributes().getNamedItem("genderId").getNodeValue());
		setRace(character.getAttributes().getNamedItem("race").getNodeValue());
		setRaceId(character.getAttributes().getNamedItem("raceId").getNodeValue());
		setRealm(character.getAttributes().getNamedItem("realm").getNodeValue());
		setSuffix(character.getAttributes().getNamedItem("suffix").getNodeValue());
		setTitleId(character.getAttributes().getNamedItem("titleId").getNodeValue());
		setClassUrl(character.getAttributes().getNamedItem("classUrl").getNodeValue());
		setFaction(character.getAttributes().getNamedItem("faction").getNodeValue());
		setFactionId(character.getAttributes().getNamedItem("factionId").getNodeValue());
		setGuildName(character.getAttributes().getNamedItem("guildName").getNodeValue());

		// Only if the character have a guild
		if (!getGuildName().equals("")) {
			setGuildUrl(character.getAttributes().getNamedItem("guildUrl").getNodeValue());	
		}
		setLastModified(character.getAttributes().getNamedItem("lastModified").getNodeValue());
		setLevel(character.getAttributes().getNamedItem("level").getNodeValue());
		setPoints(character.getAttributes().getNamedItem("points").getNodeValue());
		setPrefix(character.getAttributes().getNamedItem("prefix").getNodeValue());
		
		stats = new CharacterStats(characterXml);

		/*
		setTeamRank(character.getAttributes().getNamedItem(name).getNodeValue());
		setGuild(character.getAttributes().getNamedItem(name).getNodeValue());
		setGuildId(character.getAttributes().getNamedItem(name).getNodeValue());
		setGamesPlayed(character.getAttributes().getNamedItem(name).getNodeValue());
		setGamesWon(character.getAttributes().getNamedItem(name).getNodeValue());
		setSeasonGamesPlayed(character.getAttributes().getNamedItem(name).getNodeValue());
		setSeasonGamesWon(character.getAttributes().getNamedItem(name).getNodeValue());
		 */

	}

	public List<ArenaTeam> getArenaTeams() throws IOException, ParserConfigurationException, SAXException {
		for (int i = 0; i < characterXml.getElementsByTagName("arenaTeam").getLength(); i++) {
			Node team = characterXml.getElementsByTagName("arenaTeam").item(i);
			ArenaTeam myTeam = new ArenaTeam(armoryUrl +  "team-info.xml?" + team.getAttributes().getNamedItem("url").getNodeValue());
			arenaTeams.add(myTeam);
		}
		return arenaTeams;
	}

	
	public List<Item> getItems() throws DOMException, IOException, ParserConfigurationException, SAXException  {
		for (int i = 0; i < characterXml.getElementsByTagName("item").getLength(); i++) {
			Node item = characterXml.getElementsByTagName("item").item(i);
			items.add(new Item(armoryUrl + "item-tooltip.xml?i=" + item.getAttributes().getNamedItem("id").getNodeValue()));
		}
		return items;
	}
	 

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCharUrl() {
		return charUrl;
	}

	public void setCharUrl(String charUrl) {
		this.charUrl = charUrl;
	}

	public String getTitleId() {
		return titleId;
	}

	public void setTitleId(String titleId) {
		this.titleId = titleId;
	}

	public String getTeamRank() {
		return teamRank;
	}

	public void setTeamRank(String teamRank) {
		this.teamRank = teamRank;
	}

	public String getSuffix() {
		return suffix;
	}

	public void setSuffix(String suffix) {
		this.suffix = suffix;
	}

	public String getSeasonGamesWon() {
		return seasonGamesWon;
	}

	public void setSeasonGamesWon(String seasonGamesWon) {
		this.seasonGamesWon = seasonGamesWon;
	}

	public String getSeasonGamesPlayed() {
		return seasonGamesPlayed;
	}

	public void setSeasonGamesPlayed(String seasonGamesPlayed) {
		this.seasonGamesPlayed = seasonGamesPlayed;
	}

	public String getRealm() {
		return realm;
	}

	public void setRealm(String realm) {
		this.realm = realm;
	}

	public String getRaceId() {
		return raceId;
	}

	public void setRaceId(String raceId) {
		this.raceId = raceId;
	}

	public String getRace() {
		return race;
	}

	public void setRace(String race) {
		this.race = race;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public String getPoints() {
		return points;
	}

	public void setPoints(String points) {
		this.points = points;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getLastModified() {
		return lastModified;
	}

	public void setLastModified(String lastModified) {
		this.lastModified = lastModified;
	}

	public String getGuildUrl() {
		return guildUrl;
	}

	public void setGuildUrl(String guildUrl) {
		this.guildUrl = guildUrl;
	}

	public String getGuildName() {
		return guildName;
	}

	public void setGuildName(String guildName) {
		this.guildName = guildName;
	}

	public String getGuildId() {
		return guildId;
	}

	public void setGuildId(String guildId) {
		this.guildId = guildId;
	}

	public String getGuild() {
		return guild;
	}

	public void setGuild(String guild) {
		this.guild = guild;
	}

	public String getGenderId() {
		return genderId;
	}

	public void setGenderId(String genderId) {
		this.genderId = genderId;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getGamesWon() {
		return gamesWon;
	}

	public void setGamesWon(String gamesWon) {
		this.gamesWon = gamesWon;
	}

	public String getGamesPlayed() {
		return gamesPlayed;
	}

	public void setGamesPlayed(String gamesPlayed) {
		this.gamesPlayed = gamesPlayed;
	}

	public String getFactionId() {
		return factionId;
	}

	public void setFactionId(String factionId) {
		this.factionId = factionId;
	}

	public String getFaction() {
		return faction;
	}

	public void setFaction(String faction) {
		this.faction = faction;
	}

	public String getClassUrl() {
		return classUrl;
	}

	public void setClassUrl(String classUrl) {
		this.classUrl = classUrl;
	}

	public String getClassId() {
		return classId;
	}

	public void setClassId(String classId) {
		this.classId = classId;
	}

	public String getClassType() {
		return classType;
	}

	public void setClassType(String classType) {
		this.classType = classType;
	}

	public String getBattleGroup() {
		return battleGroup;
	}

	public void setBattleGroup(String battleGroup) {
		this.battleGroup = battleGroup;
	}

	public CharacterStats getStats() {
		return stats;
	}


}
